package core;

import java.io.*;

import fileoperation.FileCreation;
import fileoperation.FileReader;
import gitobject.*;
import repository.Repository;
public class JitLogs {

    //获取文件路径下日志记录
    static String logPath = Repository.getGitDir() + File.separator + "logs";

    // 打印指针所指向的当前commit的日志
    public static void printLogs() throws Exception {
        String branchName = JitBranch.getCurrentBranchName();
        if(branchName.length()>0){
            String commitId = FileReader.readValue(
                    new File(Repository.getGitDir() + File.separator +
                            "refs" + File.separator + "heads" + File.separator + branchName));
            if(new File(logPath).exists()){
                Commit.printLogs(Commit.deserialize(commitId));
            }else{
                initLogs();
                Commit.printLogs(Commit.deserialize(commitId));
            }
        }else{
            System.out.println("Fatal: Your Current Branch 'master' Does Not Have Any Commits Yet");
        }
    }

    public static void initLogs() throws IOException {

        /*
        * |- logs
         *   |- refs
         *      |-  heads(本地分支信息)
         *        |-  master(文件)：记录本地master分支的commit信息
         *        |-  localBranchDemo(文件)：记录本地localBranchDemo分支的commit信息
         *      |-  remote(远程分支信息)
         *       |-  master(文件)：记录远程master分支的commit信息）
         *        |-  remoteBranchDemo(文件)：记录远程remoteBranchDemo分支的commit信息）
        * */
        // log
        FileCreation.createDirectory(Repository.getGitDir(),"logs");
        // logs/refs
        FileCreation.createDirectory(Repository.getGitDir(), "logs", "refs");
        // logs/refs/heads
        FileCreation.createDirectory(Repository.getGitDir(), "logs", "refs", "heads");
        // logs/refs/heads/master
        FileCreation.createFile(Repository.getGitDir() + File.separator + "logs"+File.separator+"refs"+File.separator+"heads", "master", "");
        // logs/Head
        FileCreation.createFile(Repository.getGitDir()+File.separator+"logs","Head","");
    }
}